package com.platform.controller;

import com.platform.entity.UserAddressEntity;
import com.platform.service.UserAddressService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员管理:地址管理
 */
@RestController
@RequestMapping("/address")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("address:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserAddressEntity> userList = userAddressService.queryList(query);
        int total = userAddressService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserAddressEntity user = userAddressService.queryObject(id);

        return R.ok().put("user", user);
    }

//    /**
//     * 使用手机号用户查看信息
//     */
//    @RequestMapping("/infobyphone/{phone}")
//    @RequiresPermissions("address:info")
//    public R infobyphone(@PathVariable("phone") String phone) {
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("mobile", phone);
//        List<UserAddressEntity> userList = userAddressService.queryList(param);
//        UserAddressEntity user = null;
//        if (userList != null && userList.size() > 0)
//            user = userList.get(0);
//        return R.ok().put("user", user);
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("address:save")
    public R save(@RequestBody UserAddressEntity user) {
        userAddressService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/{id}")
    @RequiresPermissions("address:update")
    public R update(@PathVariable("id") Long id) {
        int result = userAddressService.update(id);
        if (result > 0) {
            return R.ok();
        }
        return R.error("变更失败");

    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("address:delete")
    public R delete(@PathVariable("id") Integer id) {
        int i = userAddressService.delete(id);
        if (i > 0) {
            return R.ok();
        }
        return R.error("失败");
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserAddressEntity> userList = userAddressService.queryList(params);

        return R.ok().put("list", userList);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = userAddressService.queryTotal(params);

        return R.ok().put("userSum", sum);
    }

    /**
     * 导出会员
     */
    @RequestMapping("/export")
    @RequiresPermissions("address:export")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) {

        List<UserAddressEntity> userList = userAddressService.queryList(params);

        ExcelExport ee = new ExcelExport("会员列表");

        String[] header = new String[]{"会员名称", "性别", "会员级别", "手机号码"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userList != null && userList.size() != 0) {
            for (UserAddressEntity UserAddressEntity : userList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
//                map.put("USERNAME", UserAddressEntity.getRealName());
//                map.put("GENDER", UserAddressEntity.getGender() == 0 ? "男" : (UserAddressEntity.getGender() == 1 ? "女" : "未知"));
//                map.put("LEVEL_NAME", UserAddressEntity.getId());
//                map.put("MOBILE", UserAddressEntity.getMobile());
                list.add(map);
            }
        }

        ee.addSheetByMap("会员", list, header);
        ee.export(response);
        return R.ok();
    }
}
